<?php

namespace app\api\service;

use app\admin\model\PoliteArticle;
use app\admin\model\PoliteImpress;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Exception;

class Impress
{
    /**
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws Exception
     */
    public static function getInfo($tid): array
    {
        $map = [
            ['township', '=', $tid]
        ];
        $model = new PoliteImpress();
        $row = $model->where($map)->find();
        if (!$row) {
            throw new Exception('数据不存在');
        }
        return self::resetRow($row);
    }
    public static function resetRow($row){
        if(!is_array($row)){
            $row = $row->toArray();
        }
        if(isset($row['introduce_brief'])){
            $row['introduce_brief'] = Article::getHtml($row['introduce_brief']);
        }
        if(isset($row['location_brief'])){
            $row['location_brief'] = Article::getHtml($row['location_brief']);
        }
        if(isset($row['person_brief'])){
            $row['person_brief'] = Article::getHtml($row['person_brief']);
        }
        if(isset($row['files'])){
            $row['files']=empty($row['files'])?[]:explode('|',$row['files']);
        }
        return $row;
    }
}